import pd from '/api/productData.js';
import cd from '/api/cartData.js';
import ud from '/api/usrData.js';
(() => {
    /* 加载头部导航栏 */
    $('.header-top-nav-wrap').load('/components/head-nav.html');
    /* 加载头部搜索框 并对样式进行修改 */
    $('.head').load('/components/head-search.html', function() {
        /* 隐藏位置下拉菜单 */
        $('.logo_dropdown').hide();
        /* 更改样式 */
        $('.search-bar').css('border-color', "#E12118").find('.search-btn').css('background-color', "#E12118");
        $('.search-btn').on('click', function() {
            const key = $('.search-key').val();

            // console.log(key ? "q=" + key : "title=" + $('.search-key').attr('placeholder'));
            location.href = '/pages/list.html?' + (key ? "q=" + key : "title=" + $('.search-key').attr('placeholder'));
        })
    });
    /* 加载尾部 */
    $('.bottom-footer-wrap').load('/components/bottom-footer.html');
    /* 加载右侧购物篮 */
    $('.right-bar').load('/components/right-fixed-bar.html');
    /* 渲染 */
    render();
    $('.address-list li').on('click', function() {
        $(this).addClass('check').siblings().removeClass('check');
    })
})()


function render() {

    const ckData = JSON.parse(localStorage.getItem('checkoutData'));
    if (ckData != '[]') {
        let str = ``;
        let total = 0;
        for (const key in ckData) {

            pd.getByPid(key, p => {
                str += `
                <li>
                    <a href="/pages/detial.html?pid=${p.pid}">
                        <img src='${p.imgsrc}'>
                        <p>${p.name}</p>
                    </a>
                    <p><span>￥${p.price}</span> x ${ckData[key]}</p>
                    <a>￥${parseInt(p.price * ckData[key] *100)/100}</a>
                </li>
                `;
                // console.log(parseInt(p.price * ckData[key] * 100) / 100);
                total += parseInt(p.price * ckData[key] * 100) / 100;
                $('.checkout-list').html(str);
                $('.total-span').text(total);


            })
        }
    }
    $('.pay').off('click').on('click', function() {
        const order = {
            address: $('.address-list li.check').text().replace(/\s+/g, ' '),
            order: ckData,
            time: new Date(),
            order_price: parseInt($('.total-span').text() * 100) / 100
        }

        if (localStorage.getItem('usrData') == '{}') return
        const usrData = JSON.parse(localStorage.getItem('usrData'));
        ud.getData(`/${encodeURIComponent(usrData.token)}`, data => {
            if (data['order']) {
                data['order'].push(order);
            } else {
                data['order'] = [order];
            }
            ud.updateData(`/${encodeURIComponent(usrData.token)}`, data, (data) => {
                cd.del(Object.keys(ckData))
                localStorage.removeItem('checkoutData')
                location.href = '/pages/cart.html'
            });
        })
    })
}